unit Rails.Vcl.View;

interface

uses
  Vcl.Forms, System.Generics.Collections, Vcl.ExtCtrls, Vcl.Controls, System.Classes, Vcl.Buttons,
  System.Types, Datasnap.DSProxyRest, RTTI, Rails.DsConnection, Rails.ActiveRecord.Base, Rails.Vcl.Grids, Rails.Vcl.Controls;

type
  TView<C: TActiveRecord, constructor; T: TDSRailsAdminRestClient<C>, constructor> = class(TComponent)
  private
    class var FForm: TCustomForm;
    FRecords: TObjectList<C>;
    DsProxy: T;
    FPanel: TPanel;
    FGrid: TRailsGrid;
    FBtnSave: TBitBtn;
  protected
//    procedure DoShow; Virtual;
//    procedure DoClose; Virtual;
    procedure LoadRecords; Virtual;
    function GetRecords: TObjectList<C>; Virtual;
    function GetSelectRecord: C;
    procedure BeforeBindGrid(Records: TObjectList<TActiveRecord>; Parent: TWinControl; var Position: TPoint; var AutoCreate: Boolean); virtual;
    procedure AfterBindGrid(Records: TObjectList<TActiveRecord>; Parent: TWinControl; var Position: TPoint; var AutoCreate: Boolean); virtual;
  public
    destructor Destroy; override;
    constructor Create(AOwner: TWinControl); reintroduce;
    class function FormModal(AOnwer: TComponent = nil): TCustomForm; static;
    class function Form(AOnwer: TComponent = nil): TForm; static;
    procedure Show(AParent: TWinControl);
    procedure BtnClick(Sender: TObject);
  end;

implementation

uses
  Rails.Utils, Cliente.Model, Dialogs, ClientClassesUnit1, Rails.DmMain,
  Rails.Vcl.Utils;

{ TView<T> }

procedure TView<C, T>.AfterBindGrid(Records: TObjectList<TActiveRecord>;
  Parent: TWinControl; var Position: TPoint; var AutoCreate: Boolean);
begin

end;

procedure TView<C, T>.BeforeBindGrid(Records: TObjectList<TActiveRecord>;
  Parent: TWinControl; var Position: TPoint; var AutoCreate: Boolean);
begin

end;

procedure TView<C, T>.BtnClick(Sender: TObject);
begin
  TControls<C>.Create(FPanel).Bind(GetSelectRecord);
end;

constructor TView<C, T>.Create(AOwner: TWinControl);
begin
  inherited Create(AOwner);
  FPanel := TPanel.Create(Self);
  FPanel.Parent := AOwner;
  FPanel.Align := alClient;
  FPanel.Show;
  FGrid := TRailsGrid.Create(FPanel);
  FGrid.Parent := FPanel;
  FGrid.Align := alClient;
  FGrid.ClassRecord := TRttiContext.Create.GetType(TypeInfo(C)).AsInstance.MetaclassType;
  FBtnSave := TBitBtn.Create(FPanel);
  FBtnSave.Parent := FPanel;
  FBtnSave.Align := alTop;
  FBtnSave.Caption := 'Visualizar';
  FBtnSave.OnClick := BtnClick;
  LoadRecords;
end;

destructor TView<C, T>.Destroy;
begin
  TRailsUtils.TryFreeAndNil(FForm);
  inherited;
end;

{procedure TView<C, T>.DoClose;
begin
  inherited;
end;

procedure TView<C, T>.DoShow;
begin
  inherited;
  LoadRecords;
end;}

class function TView<C, T>.Form(AOnwer: TComponent): TForm;
begin

end;

class function TView<C, T>.FormModal(AOnwer: TComponent): TCustomForm;
begin
  if not Assigned(FForm) then
    FForm := TCustomForm.Create(Form);
  FForm.ShowModal;
  Result := FForm;
end;

function TView<C, T>.GetRecords: TObjectList<C>;
begin
  Result := FRecords;
end;

function TView<C, T>.GetSelectRecord: C;
begin
  Result := C(FGrid.Objects[0, FGrid.Row]);
end;

procedure TView<C, T>.LoadRecords;
var
  E:string;
begin
  DsProxy := T.Create;
  FGrid.Records := TObjectList<TObject>(DsProxy.LoadAll(E));
  //TRailsUtilsVcl.TryMessage(E);
  //TRailsUtils.TryFreeAndNil(DsProxy);
end;

procedure TView<C, T>.Show(AParent: TWinControl);
begin
  //FGrid := TRailsGrid.Create(nil);
//  DoShow;
end;

end.

